/**
 * 核销码验证和核销功能测试
 */

const request = require('./mini/libs/request');

// 测试配置
const config = {
  baseUrl: 'http://localhost:8787', // 根据实际情况修改
  token: 'your-test-token-here' // 需要替换为实际的测试token
};

// 测试核销码
const testCode = 'ABC123DEF'; // 需要替换为实际的测试核销码

async function testCheckTicket() {
  console.log('=== 测试核销码验证 ===');
  
  try {
    const response = await request.get(`${config.baseUrl}/v1/biz-goods-order/checkTicket`, {
      needAuth: true,
      data: {
        code: testCode
      },
      headers: {
        'Authorization': `Bearer ${config.token}`
      }
    });
    
    console.log('验证结果:', response);
    
    if (response.code === 1 && !response.isError) {
      console.log('✅ 核销码验证成功');
      console.log('商品信息:', response.data);
    } else {
      console.log('❌ 核销码验证失败:', response.msg);
    }
  } catch (error) {
    console.log('❌ 验证请求失败:', error.message);
  }
}

async function testUseTicket() {
  console.log('\n=== 测试核销码使用 ===');
  
  try {
    const response = await request.post(`${config.baseUrl}/v1/biz-goods-ticket/useTicket`, {
      needAuth: true,
      data: {
        code: testCode
      },
      headers: {
        'Authorization': `Bearer ${config.token}`
      }
    });
    
    console.log('核销结果:', response);
    
    if (response.code === 1) {
      console.log('✅ 核销成功');
      console.log('核销信息:', response.data);
    } else {
      console.log('❌ 核销失败:', response.msg);
    }
  } catch (error) {
    console.log('❌ 核销请求失败:', error.message);
  }
}

async function runTests() {
  console.log('开始测试核销码功能...\n');
  
  // 先验证核销码
  await testCheckTicket();
  
  // 再测试核销
  await testUseTicket();
  
  console.log('\n测试完成');
}

// 运行测试
if (require.main === module) {
  runTests().catch(console.error);
}

module.exports = {
  testCheckTicket,
  testUseTicket
}; 